package com.xx.pmis.web.actions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Function;
import com.xx.pmis.model.Role;
import com.xx.pmis.services.FunctionService;
import com.xx.pmis.services.RoleService;
import com.xx.pmis.web.forms.RoleForm;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.Rand;

/**
 * @class 角色管理Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class RoleAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(RoleAction.class);
	RoleService roleService = getPMISServices().getRoleService();
	FunctionService functionService = getPMISServices().getFunctionService();

	// 查询所有角色
	public ActionForward listRole(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		try {
			all = roleService.getAllRole();
		} catch (Exception e) {
			log.info("查询所有角色异常！" + e.toString());
		}
		request.setAttribute("roleList", all);
		request.setAttribute("position", "系统管理/角色");
		return mapping.findForward("listRole");
	}

	// 新增角色
	public ActionForward addRole(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("roleRegDate", CurrentDate.currentDate());
		request.setAttribute("position", "角色/新增");
		return mapping.findForward("addRole");
	}

	// 新增，处理方法
	public ActionForward addRole_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		RoleForm f = (RoleForm) form;
		String roleName = f.getRoleName(); // 角色名
		String roleRegDate = f.getRoleRegDate(); // 注册日期
		String roleDesc = f.getRoleDesc(); // 描述

		Role role = new Role();
		role.setRoleName(roleName);
		// 角色编码自动产生，不能修改
		role.setRoleCode("20" + Rand.getRand());
		role.setRoleRegDate(roleRegDate);
		role.setRoleModDate(roleRegDate);
		role.setRoleState("1");
		role.setRoleDesc(roleDesc);
		try {
			roleService.saveOrUpdateRole(role);
		} catch (Exception e) {
			log.info("新增或更新角色异常！" + e.toString());
			e.printStackTrace();
		}

		listRole(mapping, form, request, response);
		return mapping.findForward("listRole");
	}

	// 编辑
	public ActionForward editRole(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String roleID = request.getParameter("roleID");

		Role role = new Role();
		try {
			role = roleService.getRoleByRoleID(roleID);
		} catch (Exception e) {
			log.info("根据角色ID查询该角色异常！" + e.toString());
		}
		request.setAttribute("roleModDate", CurrentDate.currentDate());
		request.setAttribute("role", role);
		request.setAttribute("position", "角色/编辑");
		return mapping.findForward("editRole");
	}

	// 编辑，处理方法
	public ActionForward editRole_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		RoleForm f = (RoleForm) form;
		String RoleID = f.getRoleID();
		String RoleName = f.getRoleName(); // 角色名
		String roleModDate = f.getRoleModDate(); // 修改日期
		String RoleDesc = f.getRoleDesc(); // 描述

		Role role = null;
		try {
			role = roleService.getRoleByRoleID(RoleID);
		} catch (Exception e) {
			log.info("根据角色ID查询该角色异常！" + e.toString());
		}
		role.setRoleName(RoleName);
		role.setRoleModDate(roleModDate);
		role.setRoleState("1");
		role.setRoleDesc(RoleDesc);

		try {
			roleService.saveOrUpdateRole(role);
		} catch (Exception e) {
			log.info("新增或更新角色异常！" + e.toString());
			e.printStackTrace();
		}
		listRole(mapping, form, request, response);
		return mapping.findForward("listRole");
	}

	// 删除
	public ActionForward delRole(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Role role = null;
		RoleForm f = (RoleForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				role = roleService.getRoleByRoleID(ids[i]);
			} catch (Exception e) {
				log.info("根据角色ID查询该角色异常！" + e.toString());
			}
			role.setRoleDelFlag("1");
			try {
				roleService.deleteRole(role);
			} catch (Exception e) {
				log.info("新增或更新角色异常！" + e.toString());
			}
		}

		listRole(mapping, form, request, response);
		return mapping.findForward("listRole");
	}

	// 权限分配
	public ActionForward assignFunction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String roleID = request.getParameter("roleID");
		Role role = null;
		try {
			role = roleService.getRoleByRoleID(roleID);
		} catch (Exception e) {
			log.info("根据角色ID查询该角色异常！" + e.toString());
		}
		List<?> all = null;
		try {
			all = functionService.getAllFunction();
		} catch (Exception e) {
			log.info("查询所有 包含已停用菜单异常！" + e.toString());
		}
		List<?> list = null;
		try {
			list = functionService.getAllFunction();
		} catch (Exception e) {
			log.info("查询所有菜单异常！" + e.toString());
		}
		Iterator<?> it = list.iterator();
		while (it.hasNext()) {
			Function function = (Function) it.next();
			if (role.getFunction().contains(function)) {
				function.setIsSelected("checked=\"on\"");
			} else {
				function.setIsSelected(" ");
			}
		}
		request.setAttribute("roleID", roleID);
		request.setAttribute("functionList", all);
		request.setAttribute("list", list);
		request.setAttribute("position", "角色管理/权限分配");
		return mapping.findForward("assignFunction");
	}

	// 权限分配
	public ActionForward assignFunction_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Function function = null;
		List<Function> list = new ArrayList<Function>(0);
		Role role = null;
		RoleForm f = (RoleForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				function = functionService.getFunctionByFunctionID(Integer.parseInt(ids[i]));
			} catch (Exception e) {
				log.info("根据功能ID查询该功能异常！" + e.toString());
			}
			list.add(function);

		}
		try {
			role = roleService.getRoleByRoleID(f.getRoleID());
		} catch (Exception e) {
			log.info("根据功能ID查询该功能异常！" + e.toString());
		}
		role.setFunction(list);
		try {
			roleService.saveOrUpdateRole(role);
		} catch (Exception e) {
			log.info("新增或更新角色异常！" + e.toString());
		}

		listRole(mapping, form, request, response);
		return mapping.findForward("listRole");
	}

	// 启用
	public ActionForward startRole(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String roleID = request.getParameter("roleID");

		Role role = null;
		try {
			role = roleService.getRoleByRoleID(roleID);
		} catch (Exception e) {
			log.info("根据角色ID查询该角色异常！" + e.toString());
		}
		role.setRoleState("0");
		try {
			roleService.saveOrUpdateRole(role);
		} catch (Exception e) {
			log.info("新增或更新角色异常！" + e.toString());
		}
		listRole(mapping, form, request, response);
		return mapping.findForward("listRole");
	}

	// 停用
	public ActionForward stopRole(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String roleID = request.getParameter("roleID");

		Role role = null;
		try {
			role = roleService.getRoleByRoleID(roleID);
		} catch (Exception e) {
			log.info("根据角色ID查询该角色异常！" + e.toString());
		}
		role.setRoleState("1");
		try {
			roleService.saveOrUpdateRole(role);
		} catch (Exception e) {
			log.info("新增或更新角色异常！" + e.toString());
		}
		listRole(mapping, form, request, response);
		return mapping.findForward("listRole");
	}
}